import{ajax,formatTime} from '../../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    list:[]
  },

  getIscollected: async function(e) {
    const {
      info
    } = e.currentTarget.dataset;
    const {
      title,
      author,
      cover,
      link,
      aboutSystem,
      _id,
      updateTime
    } = info
    let iscollected = e.detail;
    this.setData({
      iscollected: e.detail
    })
    if (iscollected) {
      const params2 = {
        title,
          author,
          cover,
          link,
          aboutSystem,
          id: _id,
          updateTime,
          openid:wx.getStorageSync('openid')
      }
      const result1 = await ajax('/toCollection','POST',params2);
      const {
        data
      } = result1;
      if (data === "success") {
        wx.showToast({
          title: '收藏成功！',
          icon: 'none'
        })
      }
    } else {
      //已收藏，想取消
      const params3 = {
        id:info.id,
          openid:wx.getStorageSync('openid')
      }
      const result2 = await ajax('/cancelCollection','POST',params3);
      const{data} = result2;
          if(data === "success"){
            wx.showToast({
              title: '取消成功',
              icon:"none"
            })
          }
    }
  },

  toDetail(e) {
    const {
      info
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `../articleDetail/articleDetail?info=` + encodeURIComponent(JSON.stringify(info)),
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let name = options.name;
      const params = {
        name
      };
      const result = await ajax('/searchArticle','GET',params);
      const {
        data
      } = result;
      this.setData({
        list: data.map(item => {
          return {
            ...item,
            id:item._id,
            updateTime: formatTime(item.updateTime)
          }
        })
      })

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})